package com.ssd.controller;

import com.ssd.entity.VendingMachineEntity;
import com.ssd.entity.VmTypeEntity;
import com.ssd.service.VMTypeService;
import com.ssd.utils.StringUtils;
import com.ssd.view.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/vmType")
@Slf4j
public class VMTypeController {

    @Resource
    private VMTypeService vmTypeService;

    @GetMapping("/list")
    public BaseResponse<List<VmTypeEntity>> getVMTypePage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10000") int pageSize,
            @RequestParam(required = false) String name) {
        return new BaseResponse(vmTypeService.getVMTypeList(pageNum, pageSize,name));
    }

    @GetMapping("/{id}")
    public BaseResponse <VmTypeEntity> getInfo(@PathVariable("id") Long id)
    {
        return new BaseResponse(vmTypeService.selectVmTypeById(id));
    }

    @PostMapping
    public BaseResponse<String> add(@RequestBody VmTypeEntity vmType)
    {
        return new BaseResponse(vmTypeService.insertVmType(vmType));
    }

    @PutMapping
    public BaseResponse<String> update(@RequestBody VmTypeEntity vmType) {
        return new BaseResponse(vmTypeService.updateVmType(vmType));
    }

    @DeleteMapping("/{ids}")
    public BaseResponse<String> batchDeleteVM(@PathVariable("ids") String classIdsStr) {
        Long[] classIds = StringUtils.isEmpty(classIdsStr) ?
                new Long[0] :
                Arrays.stream(classIdsStr.split(","))
                        .map(Long::parseLong)
                        .toArray(Long[]::new);
        return new BaseResponse(vmTypeService.deleteVmTypeById(classIds));
    }
}